﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.Stockstar.Quote.Model.Quote;
using System.Threading;
using SG.SDK;
using SG.SDK.Configuration;
using System.Configuration;

namespace Com.Stockstar.Quote.Common.Quote
{
    /// <summary>
    /// 行业指数
    /// </summary>
    public class IndustryIndexSingleton
    {
        //获取行业指数数据Thread
        private static Thread industryIndexThread = new Thread(new ParameterizedThreadStart(GetCodeData));
        //访问实例锁
        private static Object instanceLockObject = new Object();
        //访问代码表锁
        private static Object industryIndexLockObject = new Object();
        //行业指数列表
        private static List<IndustryIndex> industryIndexList;
        //实例
        private static IndustryIndexSingleton instance;
        //获取SG服务器地址
        private static string sgSDKServerAddress = ConfigurationSettings.AppSettings["SGSDKServerAddress"];
        //获取G服务器端口
        private static int sgSDKServerPort = Int32.Parse(ConfigurationSettings.AppSettings["SGSDKServerPort"]);
        //log4net日志
        protected static log4net.ILog logger = log4net.LogManager.GetLogger("Quote");

        private IndustryIndexSingleton()
        {

        }

        #region 实例
        /// <summary>
        /// 实例
        /// </summary>
        public static IndustryIndexSingleton Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (instanceLockObject)
                    {
                        if (instance == null)
                            instance = CreateInstance();
                    }
                }

                return instance;
            }
        }
        #endregion

        #region 创建实例
        /// <summary>
        /// 创建实例
        /// </summary>
        /// <returns></returns>
        private static IndustryIndexSingleton CreateInstance()
        {
            return new IndustryIndexSingleton();
        }
        #endregion

        #region 启动
        /// <summary>
        /// 启动
        /// </summary>
        public static void Start()
        {
            industryIndexThread.Start();
        }
        #endregion

        #region 获取代码表数据
        /// <summary>
        /// 获取代码表数据
        /// </summary>
        /// <param name="obj"></param>
        private static void GetCodeData(object obj)
        {
            while (true)
            {
                //DateTime currentTime = DateTime.Now;


                if (industryIndexList == null || industryIndexList.Count == 0)
                {
                    industryIndexList = GetIndustryIndexList();
                }
                else
                {
                    List<IndustryIndex> newIndustryIndexList = GetIndustryIndexList();
                    if (newIndustryIndexList != null && newIndustryIndexList.Count > 0)
                    {
                        industryIndexList = newIndustryIndexList;
                    }
                }

                Thread.Sleep(60000);
            }
        }
        #endregion

        #region 获取行业指数
        /// <summary>
        /// 获取行业指数
        /// </summary>
        /// <returns></returns>
        private static List<IndustryIndex> GetIndustryIndexList()
        {
            List<IndustryIndex> dataList = new List<IndustryIndex>();
            try
            {

                SDKClient client = new SDKClient(new ResourceAddress(sgSDKServerAddress, sgSDKServerPort), "FEED_INDUINDEX_DAYK");
                //获取数据

                SGObject[] array = client.GetRows();
                if (array != null && array.Length > 0)
                {
                    foreach (SGObject data in array)
                    {
                        IndustryIndex industryIndex = new IndustryIndex();
                        //行业代码
                        industryIndex.Code = data["CODE"].ToString();
                        //行业名称
                        industryIndex.Name = data["SCNAME"].ToString();
                        //行业级别
                        industryIndex.Level = data["LEVEL"].NullIntValue();
                        //昨收
                        industryIndex.PreClosePrice = data["PRECLOSE"].NullDoubuleValue();
                        //K值
                        industryIndex.KValue = data["KVALUE"].NullDoubuleValue();
                        //收盘价
                        industryIndex.ClosePrice = data["CLOSE"].NullDoubuleValue();
                        //总市值
                        industryIndex.MarketValue = data["MKCAP"].NullDoubuleValue();
                        //交易日
                        industryIndex.TradeDate = data["TRADEDAY"].NullIntValue();
                        //成交量(A股)
                        industryIndex.Volume = data["VOLUME"].NullDoubuleValue();
                        //成交额(A股)
                        industryIndex.Amount = data["VALUE"].NullDoubuleValue();
                        //成分股数量
                        industryIndex.StockQuantity = data["STOCKNUM"].NullIntValue();
                        //上涨家数
                        industryIndex.UpQuantity = data["UPNUM"].NullIntValue();
                        //下跌家数
                        industryIndex.DownQuantity = data["DOWNNUM"].NullIntValue();
                        //平盘家数
                        industryIndex.EqualQuantity = data["EQNUM"].NullIntValue();

                        //A股成份股数量
                        industryIndex.StockAQuantity = data["STOCKNUM_A"].NullIntValue();
                        //A股上涨家数
                        industryIndex.StockAUpQuantity = data["UPNUM_A"].NullIntValue();
                        //A股下跌家数
                        industryIndex.StockADownQuantity = data["DOWNNUM_A"].NullIntValue();
                        //A股平盘家数
                        industryIndex.StockAEqualQuantity = data["EQNUM_A"].NullIntValue();
                        //上涨A股平均涨跌幅
                        industryIndex.StockAAvgUpQuantity = data["UPZDFAVG_A"].NullDoubuleValue();
                        //下跌A股平均涨跌幅
                        industryIndex.StockAAvgDownQuantity = data["DOWNZDFAVG_A"].NullDoubuleValue();
                        //A股成份股均价
                        industryIndex.StockAAvgPrice = data["PRICEAVG_A"].NullDoubuleValue();
                        //A股平均张跌额
                        industryIndex.StockAAvgUpDownValue = data["ZDAVG_A"].NullDoubuleValue();
                        //A股平均涨跌幅
                        industryIndex.StockAAvgUpDownRate = data["ZDFAVG_A"].NullDoubuleValue();
                        //A股总市值
                        industryIndex.StockAMarketValue = data["MKCAP_A"].NullDoubuleValue();

                        //B股成份股数量
                        industryIndex.StockBQuantity = data["STOCKNUM_B"].NullIntValue();
                        //B股上涨家数
                        industryIndex.StockBUpQuantity = data["UPNUM_B"].NullIntValue();
                        //B股下跌家数
                        industryIndex.StockBDownQuantity = data["DOWNNUM_B"].NullIntValue();
                        //B股平盘家数
                        industryIndex.StockBEqualQuantity = data["EQNUM_B"].NullIntValue();
                        //上涨B股平均涨跌幅
                        industryIndex.StockBAvgUpQuantity = data["UPZDFAVG_B"].NullDoubuleValue();
                        //下跌B股平均涨跌幅
                        industryIndex.StockBAvgDownQuantity = data["DOWNZDFAVG_B"].NullDoubuleValue();
                        //B股成份股均价
                        industryIndex.StockBAvgPrice = data["PRICEAVG_B"].NullDoubuleValue();
                        //B股平均张跌额
                        industryIndex.StockBAvgUpDownValue = data["ZDAVG_B"].NullDoubuleValue();
                        //B股平均涨跌幅
                        industryIndex.StockBAvgUpDownRate = data["ZDFAVG_B"].NullDoubuleValue();
                        //B股总市值
                        industryIndex.StockBMarketValue = data["MKCAP_B"].NullDoubuleValue();

                        //最高
                        industryIndex.HightPrice = data["HIGH"].NullDoubuleValue();
                        //最低
                        industryIndex.LowestPrice = data["LOW"].NullDoubuleValue();
                        //涨跌额
                        industryIndex.UpDownValue = data["ZD"].NullDoubuleValue();
                        //涨跌幅
                        industryIndex.UpDownRate = data["ZDF"].NullDoubuleValue();
                        //父板块统一编码
                        industryIndex.FatherPlateCode = data["HRLVLSCUNIC"].NullIntValue();
                        //板块分类体系参数编码
                        industryIndex.PlateCategoryCode = data["SCCLSSYSREFC"].NullIntValue();

                        //更新时间
                        industryIndex.UpdateTime = data["MTIME"].NullDateTimeValue();

                        dataList.Add(industryIndex);
                    }
                }


                //int count = dataList != null ? dataList.Count : 0;
                //logger.Info(String.Format("获取行业指数成功！数据个数{0}", count));
            }

            catch (Exception ex)
            {
                logger.Error("从SG.SDK接口获取行业指数出错！", ex);
                return null;
            }

            return dataList;

        }
        #endregion

        #region 行业指数列表
        /// <summary>
        /// 行业指数列表
        /// </summary>
        public List<IndustryIndex>IndustryIndexList
        {
            get
            {
                if (industryIndexList == null || industryIndexList.Count == 0)
                {
                    lock (industryIndexLockObject)
                    {
                        if (industryIndexList == null || industryIndexList.Count == 0)
                            industryIndexList = GetIndustryIndexList();
                    }
                }
                return industryIndexList;
            }
        }
        #endregion
    }
}
